(function($) {
    var arkflow = $.arkflow;

    // 矩形
    arkflow.FlowNode = Base.extend({
        constructor: function(states, paper) {
            var me = this;

            this.id = "workflowNode" + arkflow.util.nextId();
            this.bindNode = $.extend(true, {}, arkflow.config.rect, states);
            this.paper = paper;

            this.init();

            this.initDrag();

            this.resizer = new arkflow.Resizer(this.bindNode, this.paper, this.resetNode.bind(this));

            this.bindDrawPath();

            $(this.paper).bind("click", this.changeNode.bind(this));

            $(this.paper).bind("textchange", this.textChange.bind(this));

            this.resetNode();
        },
        // 偏移位置计算
        offset : function(x, y) {
            if (!arkflow.config.editable) {
                return;
            }
            var newX = (this.point_x + x);
            var newY = (this.point_y + y);
            this.resizer.resizeInfo.x = newX - this.bindNode.margin;
            this.resizer.resizeInfo.y = newY - this.bindNode.margin;

            this.resetNode();
        },
        // 显示节点
        showNode : function() {
            this.point_x = this.border.attr("x");
            this.point_y = this.border.attr("y");
            this.border.attr({
                opacity : 0.5
            });
            this.image.attr({
                opacity : 0.5
            });
            this.textText.attr({
                opacity : 0.5
            });
        },
        // 隐藏节点
        hidenNode : function() {
            this.border.attr({
                opacity : 1
            });
            this.image.attr({
                opacity : 1
            });
            this.textText.attr({
                opacity : 1
            });
        },
        checkDrawPath : function() {
            if (!arkflow.config.editable) {
                return;
            }
            this.resizer.show();
            var mod = $(this.paper).data("mod");
            switch (mod) {
                case "pointer" :
                    break;
                case "path" :
                    var rect = $(this.paper).data("bindNode");
                    if (rect && rect.getId() != this.id
                        && rect.getId().substring(0, 12) == "workflowNode") {
                        $(this.paper).trigger("addpath", [rect, this]);
                    }
                    break;
            }
            $(this.paper).trigger("click", this);
            $(this.paper).data("bindNode", this);
            return false;
        },
        // 添加个元素连线方法
        bindDrawPath : function() {
            $([this.border.node, this.textText.node,
                this.nameText.node, this.image.node]).bind("click", this.checkDrawPath.bind(this));
        },
        // 切换选中节点，属性更改、周围选择提示框更改
        changeNode : function(event, node) {
            if (!arkflow.config.editable) {
                return;
            }
            if (node.getId() == this.id) {
                $(this.paper).trigger("showprops", [this.bindNode.props, node]);
            } else {
                this.resizer.hide();
            }
        },
        // 节点text值更改
        textChange : function(event, newText, node) {
            if (node.getId() == this.id) {
                this.textText.attr({
                    text : newText
                });
            }
        },
        createBorder: function() {
            this.border = this.paper.rect(this.bindNode.attr.x,
                    this.bindNode.attr.y, this.bindNode.attr.width,
                    this.bindNode.attr.height, this.bindNode.attr.r).hide()
                .attr(this.bindNode.attr);
        },
        createImage: function( ) {
            this.image = this.paper.image(
                arkflow.config.basePath + this.bindNode.img.src,
                this.bindNode.attr.x + this.bindNode.img.width / 2,
                this.bindNode.attr.y
                    + (this.bindNode.attr.height - this.bindNode.img.height)
                    / 2, this.bindNode.img.width,
                this.bindNode.img.height).hide();
        },
        createNameText: function() {
            // name : {text:'<<start>>'}
            this.nameText = this.paper.text(
                    this.bindNode.attr.x
                        + this.bindNode.img.width
                        + (this.bindNode.attr.width - this.bindNode.img.width)
                        / 2,
                    this.bindNode.attr.y + arkflow.config.lineHeight / 2,
                    this.bindNode.name.text).hide()
                .attr(this.bindNode.name);
        },
        createTextText: function() {
            // text : {text:'开始'}
            this.textText = this.paper.text(
                    this.bindNode.attr.x
                        + this.bindNode.img.width
                        + (this.bindNode.attr.width - this.bindNode.img.width)
                        / 2,
                    this.bindNode.attr.y
                        + (this.bindNode.attr.height - arkflow.config.lineHeight)
                        / 2 + arkflow.config.lineHeight,
                    this.bindNode.text.text).hide()
                .attr(this.bindNode.text);
        },
        init : function() {
            this.createBorder();
            this.createImage();
            this.createNameText();
            this.createTextText();
        },
        initDrag : function() {
            var me = this;
            // 各元素drag事件添加
            this.parts = [this.border, this.image, this.nameText, this.textText];
            for(var i=0; i<this.parts.length; i++) {
                this.parts[i].drag(function(x, y) {
                    me.offset(x, y);
                }, function() {
                    me.showNode();
                }, function() {
                    me.hidenNode();
                });
            }
        },
        restore : function(node) {
            this.bindNode = $.extend(true, this.bindNode, node);
            this.textText.attr({
                text : node.text.text
            });
            this.resetNode();
        },
        // 节点状态更改，重置属性并显示
        resetNode : function() {
            var x = this.resizer.resizeInfo.x + this.bindNode.margin;
            var r = this.resizer.resizeInfo.y + this.bindNode.margin;
            var width = this.resizer.resizeInfo.width - this.bindNode.margin * 2;
            var height = this.resizer.resizeInfo.height - this.bindNode.margin * 2;
            this.border.attr({
                x : x,
                y : r,
                width : width,
                height : height
            });
            // 根据不同显示配置，显示图形、文本或图形+文本
            switch (this.bindNode.showType) {
                case "image" :
                    this.image.attr({
                        x : x + (width - this.bindNode.img.width) / 2,
                        y : r + (height - this.bindNode.img.height) / 2
                    }).show();
                    break;
                case "text" :
                    this.border.show();
                    this.textText.attr({
                        x : x + width / 2,
                        y : r + height / 2
                    }).show();
                    break;
                case "image&text" :
                    this.border.show();
                    this.nameText.attr({
                        x : x + this.bindNode.img.width
                            + (width - this.bindNode.img.width) / 2,
                        y : r + arkflow.config.lineHeight / 2
                    }).show();

                    this.textText.attr({
                        x : x + this.bindNode.img.width
                            + (width - this.bindNode.img.width) / 2,
                        y : r + (height - arkflow.config.lineHeight) / 2
                            + arkflow.config.lineHeight
                    }).show();

                    this.image.attr({
                        x : x + this.bindNode.img.width / 2,
                        y : r + (height - this.bindNode.img.height) / 2
                    }).show();
                    break;
            }

            this.resizer.redraw();

            $(this.paper).trigger("rectresize", this);
        },
        // 流程数据转JSON字符串
        toJson : function() {
            var result = "{type:'" + this.bindNode.type + "',text:{text:'"
                + this.textText.attr("text") + "'}, attr:{ x:"
                + Math.round(this.border.attr("x")) + ", y:"
                + Math.round(this.border.attr("y")) + ", width:"
                + Math.round(this.border.attr("width")) + ", height:"
                + Math.round(this.border.attr("height")) + "}, props:{";
            for (var prop in this.bindNode.props) {
                result += prop + ":{value:'" + this.bindNode.props[prop].value
                    + "'},";
            }
            if (result.substring(result.length - 1, result.length) == ",") {
                result = result.substring(0, result.length - 1);
            }
            result += "}}";
            return result;
        },
        getId : function() {
            return this.id;
        },
        getBBox : function() {
            return this.resizer.resizeInfo;
        },
        attr : function(args) {
            if (args) {
                this.border.attr(args);
            }
        },
        text : function() {
            return this.textText.attr("text");
        },
        // 各元素移除
        remove : function() {
            this.textText.remove();
            this.nameText.remove();
            this.image.remove();
            this.border.remove();

            this.resizer.destory();
        }
    });

})(jQuery);